package com.zmn.brs.task.listenter.order;

import com.alibaba.fastjson.JSONObject;
import com.zmn.brs.business.interfaces.order.OrderQuantityBService;
import com.zmn.brs.common.constant.BrsMqConst;
import com.zmn.brs.common.enums.EventTypeEnum;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.common.dto.QueueOrderNodeNotifyDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 类描述：订单监控整体单量消息监听
 *
 * @author chongw
 * @since 2021/06/25 17:52
 */
@Slf4j
@Component
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
public class OrderQuantityMessageQueueListener {

    @Resource
    OrderQuantityBService orderQuantityBService;

    final static String TAG = "OMS订单下单消息监听";

    @AliyunMQHandler(topic = "${spring.mq.topic.oms}", tags = BrsMqConst.MESSAGE_ORDER_ASSIGN)
    public void handler(String message) {
        log.info("[{}],接收到新的消息message,原始消息:{}", TAG, message);
        try {
            QueueOrderNodeNotifyDTO queueOrderNodeNotifyDTO = JSONObject.parseObject(message, QueueOrderNodeNotifyDTO.class);
            Boolean flag = orderQuantityBService.compute(EventTypeEnum.QUANTITY.getTypeId(), queueOrderNodeNotifyDTO);
            if (flag) {
                log.info("[{}], 消息处理成功！！！", TAG);
                return;
            }
            log.error("[{}], 消息处理失败！！！，消息体:{}", TAG, message);
        }catch (Exception e){
            log.error("[{}], 消息处理异常！！！，消息体:{}，异常信息：", TAG, message, e);
        }
    }

}
